package cn.lzx.service.impl;


import cn.lzx.domain.AjaxResult;
import cn.lzx.domain.Product;
import cn.lzx.domain.Store;
import cn.lzx.dto.ProductDto;
import cn.lzx.feign.StoreService;
import cn.lzx.mapper.ProductMapper;
import cn.lzx.service.ProductService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StoreService storeService;

    @Override
    public AjaxResult add(ProductDto productDto) {

        try {

            Product product = new Product();
            product.setName(productDto.getName());
            product.setPrice(productDto.getPrice());
            productMapper.add(product); // 开启返回主键到id字段


            Store store = new Store();
            store.setProductId(product.getId());
            store.setNumber(productDto.getNumber());

           AjaxResult add = storeService.add(store);
            if (!add.getSuccess()){ // 当失败的时候才进来
                return add;
            }
            return new AjaxResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败，请重试！");
        }

    }
}
